/*
 * @Author: your name
 * @Date: 2021-08-02 01:30:11
 * @LastEditTime: 2021-10-09 16:06:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \aircityinit\src\tools\Event.js
 */

import store from "../store";
let polygonTypesData = []
const OnEvent = async (e) => {
  console.log(e);
  // 判断是否为左键点击的交互
  if (e.eventtype === "LeftMouseButtonClick") {
    console.log(e, 'e');
    // switch开关后的面高亮
    if (e.Type === "Polygon" && e.GroupID === "switch_Polygon") {
      console.log("点击的是面", JSON.parse(e.UserData).type);
      console.log(store.state.Sjsx.polygonType, "vuex里的Type");

      store.dispatch("Sjsx/setHighlightLoading_act", true)
      // 类型的过滤
      let polygonTypes = await store.state.Sjsx.polygonType.filter(item => {
        // console.log(item,"item的值");
        // 当item.userData的值为null时，即为其他类型
        // if (item.userData == null) {
        //   item.userData = ''
        // }
        // console.log(JSON.parse(item.userData).type, "item.userData.type的值");
        return JSON.parse(item.userData).type == JSON.parse(e.UserData).type;
      })
      console.log(polygonTypes, "polygonTypes的值");

      // 让点击的图层高亮
      console.log(polygonTypesData, "一开始进来的polygonTypesData");
      let beforePolygonId = []  // 保留的上一个高亮图层的id
      let currentPolygonId = [] // 保留当前高亮图层的id
      polygonTypesData.forEach(item => {
        beforePolygonId.push(item.id)
      })
      console.log(beforePolygonId, "上一个高亮面的id");
      await __g.polygon.delete(beforePolygonId)

      polygonTypesData = []
      // await __g.highlightArea.clear();
      polygonTypes.forEach((element, index) => {
        // let o = {
        //   id: element.id + "_" + index,
        //   coordinates: element.coordinates,
        //   color: [1, 0, 0, 0.8],      //多边形高亮颜色
        //   heightRange: [0.0, 300.0],  //高亮染色区域可以限定一个高度范围，也就是Z坐标的区间，只有Z值这这个区间的模型才会被染色
        //   intensity: 10.0,             //高亮颜色的强度
        //   depthTest: false,             //深度检测
        // };
        // console.log(element.coordinates,"KKKKKKKKKKKKKKKKKKKKKK");

        // 让高亮的面的高度为1，避免出现被遮挡
        element.coordinates && element.coordinates.forEach(item => {
          if (item instanceof Array && item[0] instanceof Array && item[0][0] instanceof Array) {
            item.forEach(mon => {
              // console.log(mon, "mon的值--KKK");
              mon.forEach(element => {
                // console.log(element[2], "element的值--KKK");
                element[2] = 1
              })
            })
          } else {
            // console.log(item, "item的值--KKK");
            item[2] = 1
          }
        })

        let o = {
          id: element.id + "_" + index,
          coordinates: element.coordinates,
          color: [1, 0, 0, 0.8],
          frameColor: [1, 1, 0, 0.8], //边框颜色
          frameThickness: 1, //边框厚度
          intensity: 1, //亮度
          // style: window.PolygonStyle.SingleColor, //单色 请参照API开发文档选取枚举
          style: 0,
          depthTest: false, //是否做深度检测
          //   coordinateType: 1,
        };
        polygonTypesData.push(o)
      })

      // 循环保存当前高亮图层的id
      polygonTypesData.forEach(item => {
        currentPolygonId.push(item.id)
      })
      console.log(currentPolygonId, "当前高亮面的id");
      store.dispatch("Sjsx/setPolygonTypeId_act", currentPolygonId)

      console.log(polygonTypesData, "polygonTypesData的值");
      await __g.polygon.add(polygonTypesData);
      // await __g.highlightArea.add(polygonTypesData);
      store.dispatch("Sjsx/setHighlightLoading_act", false)

    }
    // 图层树现状及规划的弹窗信息
    if(e.Type === "Polygon" && e.GroupID === "geojson"){
      console.log("点击的是图层树的面", JSON.parse(e.UserData));
      store.dispatch("Sjsx/setIsShowBoune_act", true)
      store.dispatch("Sjsx/setBounceData_act", JSON.parse(e.UserData))

    }

  }
  if (e.eventtype === "CameraMoving") {
    // console.log(e, 'e');
    if (e.Position[2] <= 2000) {
      __g.infoTree.hideByGroupId('line1');
      __g.infoTree.hideByGroupId('line3');
    } else {
      __g.infoTree.showByGroupId('line1');
      __g.infoTree.showByGroupId('line3');
    }
  }
  if (e.eventtype === "CameraTourFinished") {
    console.log("导览播放结束");
  }
};
export default OnEvent;
